package com.example.testcamara;

import android.content.Context;
import android.text.TextUtils;

/**
 * 统一对外接口，ajx可用协议路径解析
 *
 * 输入：目前ajx路径中各种可用协议, 例如：sdcard://xxx, app://xxx, file://aui_root,  file:///aui_root等
 *
 * @author liupo
 * @date 2019/04/25
 */

public class PathUtil {
    private static final String DOMAIN_SDCARD = "sdcard://";
    private static final String DOMAIN_APP = "app://";
    private static final String DOMAIN_FILE = "file://";
    private static String SDCARD_PATH;
    private static String APP_PATH;
    private static boolean isInitialed;

    /**
     * 统一处理路径方法封装
     */
    public static String processPath(Context context, String path) {
        if (!initIfNeeded(context)) {
            // 初始化无法完成直接异常提醒
            return null;
        }

        // 如果是sdcard:// 或者 app://, 提前处理
        final String newPath = processPathInternal(path);
        if (newPath == null) {
            return null;
        }
        return null;
//        return Ajx.getInstance().lookupLoader(newPath).processingPath(PictureParams.make(ajxContext, newPath, false));
    }

    private static boolean initIfNeeded(Context context) {
        if (!isInitialed && context != null) {
            SDCARD_PATH = context.getExternalFilesDir(null).getAbsolutePath() + "/";
            APP_PATH = context.getFilesDir().getPath() + "/";
            isInitialed = true;
        }
        return true;
    }

    private static String processPathInternal(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        } else if (path.startsWith(DOMAIN_SDCARD)) {
            return DOMAIN_FILE + SDCARD_PATH + path.substring("sdcard://".length());
        } else if (path.startsWith(DOMAIN_APP)) {
            return DOMAIN_FILE + APP_PATH + path.substring("app://".length());
        }
        return path;
    }
}
